﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;

namespace Sybercare_ProductTool_WriteSn.Msc
{
    class IniFile
    {
        public string path;

        public IniFile(string path)
        {
            this.path = path;
        }

        #region 声明读写INI文件的API函数
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defval, StringBuilder retval, int size, string filepath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string defval, Byte[] retval, int size, string filepath);

        [DllImport("KERNEL32.DLL ", EntryPoint = "GetPrivateProfileSection", CharSet = CharSet.Ansi)]
        public static extern int GetPrivateProfileSection(string lpAppName, byte[] lpReturnedString, int nSize, string filePath);

        #endregion

        /// <summary>
        /// 写INI文件
        /// </summary>
        /// <param name = "section">段落</param>
        /// <param name = "key">键</param>
        /// <param name = "value">值</param>
        public void IniWriteValue(string section, string key, string val)
        {
            WritePrivateProfileString(section, key, val, this.path);
        }

        /// <summary>
        /// 读取INI文件     
        /// </summary>
        /// <param name="section">段落</param>
        /// <param name="key">键</param>
        /// <returns>返回的键值</returns>
        public string IniReadValue(string section, string key)
        {
            StringBuilder tmp = new StringBuilder(255);

            int i = GetPrivateProfileString(section, key, "", tmp, 255, this.path);
            return tmp.ToString();
        }

        /// <summary>
        /// 读取INI文件
        /// </summary>
        /// <param name="Section">段，格式[]</param>
        /// <param name="Key">键</param>
        /// <returns>返回byte类型的section组或键值组</returns>
        public byte[] IniReadValues(string section, string key)
        {
            byte[] tmp = new byte[255];

            int i = GetPrivateProfileString(section, key, "", tmp, 255, this.path);
            return tmp;
        }

        public int GetAllKeyValues(string section, out string[] keys, out string[] vals)
        {
            byte[] b = new byte[65535];

            GetPrivateProfileSection(section, b, b.Length, this.path);

            string s = System.Text.Encoding.Default.GetString(b);
            string[] tmp = s.Split((char)0);

            ArrayList result = new ArrayList();

            foreach (string r in tmp)
            {
                if (r != string.Empty)
                {
                    result.Add(r);
                }
            }

            keys = new string[result.Count];
            vals = new string[result.Count];
            for (int i = 0; i < result.Count; i++)
            {
                string[] item = result[i].ToString().Split(new char[] { '=' });
                if (item.Length == 2)
                {
                    keys[i] = item[0].Trim();
                    vals[i] = item[1].Trim();
                }
                else if (item.Length == 1)
                {
                    keys[i] = item[0].Trim();
                    vals[i] = "";
                }
                else if (item.Length == 0)
                {
                    keys[i] = "";
                    vals[i] = "";
                }
            }

            return 0;

        }
    }
}
